<?php

namespace Apps\AppApi\Method\Question;

use Apps\AppApi\Method\BaseMethod;
use PhalconPlus\Api\Common\ResultStatus;
use Apps\Common\Models\Question;
use Apps\Common\Models\QuestionTag;
use Apps\Common\Libs\PicUrl;

/**
 * 问答详情的相关问答
 */
class QuestionRelevant extends BaseMethod
{
    public function run()
    {
        $question_id = intval($this->getDataItem('question_id',0));

        if ($question_id < 1) {
            return $this->errorLog(ResultStatus::URL_PARAM_CANNOT_EMPTY, '参数错误！');
        }

        $QuestionTag = new QuestionTag();

        if ($tagList = $QuestionTag->query()->where('question_id = ' . $question_id)->execute()->toArray()) {
            $tag_id = '';
            foreach ($tagList as $val) {
                $tag_id .= $val['tag_id'] . ',';
            }
            $tag_id = '(' . trim($tag_id, ',') . ')';

            $question_id_list = $QuestionTag->query()->groupBy('question_id')->where("tag_id in{$tag_id} and question_id != {$question_id}")->orderBy('id DESC')->limit(20)->execute()->toArray();

            if ($question_id_list) {

                shuffle($question_id_list);
                $where_question_id = '';
                foreach ($question_id_list as $val) {
                    $where_question_id .= $val['question_id'] . ',';
                }
                $where_question_id = '(' . trim($where_question_id, ',') . ')';

                $field = [
                    'question_id', 'question_content', 'question_pics',
                    'question_tag', 'answer_num', 'question_browsers',
                    'question_addtime', 'u.user_id', 'u.user_nickname',
                    'u.user_expert', 'u.user_cover', 'u.user_gender',
                ];
                $field = implode(',', $field);
                $sql = "SELECT {$field} FROM question q LEFT JOIN user_base u ON u.user_id = q.user_id WHERE q.question_id in{$where_question_id} AND q.question_state = 1 ORDER BY RAND() LIMIT 3";

                $result = (new Question())->getReadConnection()->query($sql);

                $list = $result->fetchAll();

                foreach ($list as $k => $val) {

                    foreach ($val as $key => $v) {
                        if (is_numeric($key)) {
                            unset($list[$k][$key]);
                        }
                    }

                    $list[$k]['question_tag'] = $val['question_tag'] ? json_decode($val['question_tag'], true) : [];
                    $list[$k]['user_cover'] = PicUrl::UserCover($val['user_cover'], $this->getDi());
                    $list[$k]['question_content'] = unserialize(base64_decode($val['question_content'])) ?: $val['question_content'];

                    if ($val['question_pics']) {
                        $question_pics = [];
                        foreach (json_decode($val['question_pics'], true) as $key => $value) {
                            $question_pics[] = PicUrl::Question($value, $this->getDi());
                        }
                        $list[$k]['question_pics'] = $question_pics;
                    }
                }
                $this->setResult($list);
            }
        }
    }

}